package com.sgcc.fj.pmos.e_fj_mall.activity;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.sgcc.fj.pmos.arouter.mall.MallArouterPath;
import com.sgcc.fj.pmos.common.base.MvpBaseRecycleActivity;
import com.sgcc.fj.pmos.common.utils.DialogUtil;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.e_fj_mall.adapter.InvitationListAdapter;
import com.sgcc.fj.pmos.e_fj_mall.bean.InvitationListBean;
import com.sgcc.fj.pmos.e_fj_mall.contract.InvitationListContract;
import com.sgcc.fj.pmos.e_fj_mall.presenter.InvitationListPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐邀约
 */
@Route(path = MallArouterPath.MALL_INVITATIONLIST)
public class InvitationListActivity extends MvpBaseRecycleActivity<InvitationListBean.DataDTO.ListDTO, InvitationListPresenter> implements InvitationListContract.View {

    private TitleBar mTitleBar;
    private RelativeLayout mRlReleaseInvitation;

    private InvitationListAdapter mInvitationListAdapter;
    private int state = 0;


    @Override
    protected InvitationListPresenter createPresenter() {
        return new InvitationListPresenter();
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mInvitationListAdapter = new InvitationListAdapter();
        mInvitationListAdapter.setOnBtnClickListener(new InvitationListAdapter.OnBtnClickListener() {
            @Override
            public void deleteBtn(int position, InvitationListBean.DataDTO.ListDTO data) {
                DialogUtil.showConfirm(mContext, "提示", "是否删除此条意向", new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        mPresenter.deleteInvitation(data.getGuid());
                    }
                });
            }

            @Override
            public void editBtn(int position, InvitationListBean.DataDTO.ListDTO data) {
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_ISSUEDINVITATION)
                        .withSerializable("invitationData", data)
                        .navigation(InvitationListActivity.this, 0x135);

            }

            @Override
            public void upOrDownBtn(int position, InvitationListBean.DataDTO.ListDTO data) {
                state = data.getStauts();
                if (data.getStauts() == 0)
                    for (InvitationListBean.DataDTO.ListDTO datum : mInvitationListAdapter.getData()) {
                        if (datum.getStauts() == 1) {
                            ToastUtils.showLong("已有发布中的意向，无法继续发布。");
                            return;
                        }
                    }

                DialogUtil.showConfirm(mContext, "提示",
                        String.format("是否%s此条意向", data.getStauts() == 1 ? "撤销" : "发布"),
                        new OnConfirmListener() {
                            @Override
                            public void onConfirm() {

                                int flag = data.getStauts() == 1 ? 0 : 1;
                                mPresenter.updateStatusInvitation(data.getGuid(), flag,
                                        data.getIntentionStartDate(), data.getIntentionEndDate());

                            }
                        });
            }
        });
        return mInvitationListAdapter;
    }

    @Override
    protected void initData() {
        super.initData();
        title_bar.setTitle("意向管理");
        title_bar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });

        floating_btn.setImageResource(R.mipmap.ic_add1);
    }

    @Override
    public void setOnFloatingButtonClickListener() {
        ARouter.getInstance()
                .build(MallArouterPath.MALL_ISSUEDINVITATION)
                .navigation(InvitationListActivity.this, 0x135);

    }

    @Override
    public boolean getStandardSmartLoad() {
        return false;
    }

    @Override
    public void onChangeCustomPage() {
        super.onChangeCustomPage();
        View notLoginView = getLayoutInflater().inflate(R.layout.not_data_invitation_layout, not_login_layout, false);
        findViewByIds(notLoginView);
        not_login_layout.addView(notLoginView);

    }

    private void findViewByIds(View notLoginView) {

        mTitleBar = (TitleBar) notLoginView.findViewById(R.id.title_bar);
        mTitleBar.setTitle("意向管理");
        mTitleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });
        mRlReleaseInvitation = (RelativeLayout) notLoginView.findViewById(R.id.rl_release_invitation);

        mRlReleaseInvitation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_ISSUEDINVITATION)
                        .navigation(InvitationListActivity.this, 0x135);
            }
        });
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mPresenter.queryInvitationList();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0x135) {
                refresh(true);
            }
        }
    }

    @Override
    public void queryInvitationListSuccess(InvitationListBean.DataDTO response) {
        if (null != response.getList()) {
            loadSuccess(false, response.getList());
            floating_btn.setVisibility(response.getList().size() > 0 ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void deleteInvitationSuccess() {
        refresh(true);

    }

    @Override
    public void updateStatusInvitationSuccess() {
        ToastUtils.showLong((state == 1 ? "撤销" : "发布") + "成功");
        refresh(true);
    }

    @Override
    public void queryInvitationListFailed() {

    }
}
